package com.jingdong.dao.address.impl;

import com.jingdong.dao.address.CityDao;
import com.jingdong.model.City;
import com.jingdong.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CityDaoImpl implements CityDao {
    public City getCityById(int id) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            City city=sqlSession.selectOne("com.landeqing.model.cityModel.getCityById",id);
            return city;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public List<City> getCityList(int id) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();

            List<City> cityList=sqlSession.selectList("com.landeqing.model.cityModel.getCityList",id);
            return cityList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public int citySave(City city) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            int result=sqlSession.insert("com.landeqing.model.cityModel.citySave",city);
            sqlSession.commit();
            sqlSession.close();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
    //根据名字从市查询得到省和市的id
    public City getCityForLikeByName(String stringCity) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
          List<City>cityList=  sqlSession.selectList("com.landeqing.model.cityModel.getCityForLikeByName",stringCity);
          if(cityList!=null&&cityList.size()>0){
              return cityList.get(0);
          }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String [] args){
        CityDaoImpl cityDao=new CityDaoImpl();
        City city=cityDao.getCityForLikeByName("张家口市");
        System.out.println(city.getId()+"-------------"+city.getP_id());
    }
}
